package vip.xiaomaoxiaoke.functiondemo.config;

import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import vip.xiaomaoxiaoke.functiondemo.msg.interceptor.LocalMessageAnnotationInterceptor;
import vip.xiaomaoxiaoke.functiondemo.msg.repository.LocalMessageTable;
import vip.xiaomaoxiaoke.functiondemo.msg.strategies.RefusalStrategies;
import vip.xiaomaoxiaoke.functiondemo.msg.service.ReliableMethodExecuteService;
import vip.xiaomaoxiaoke.functiondemo.msg.annotate.LocalMessage;
import vip.xiaomaoxiaoke.functiondemo.msg.repository.LocalMessageRepository;

@Configuration
@EnableAspectJAutoProxy
public class Config {

    @Bean
    public LocalMessageAnnotationInterceptor localMessageAnnInterceptor(@Autowired ReliableMethodExecuteService reliableMessageSendService) {
        return  new LocalMessageAnnotationInterceptor(reliableMessageSendService);
    }

    @Bean
    public ReliableMethodExecuteService reliableMessageSendService(@Autowired LocalMessageRepository localMessageRepository) {
        return new ReliableMethodExecuteService(localMessageRepository, new RefusalStrategies() {
            @Override
            public void execute(LocalMessageTable localMessageTable) {
                System.out.println(localMessageTable);
            }
        });
    }

    @Bean
    public PointcutAdvisor asyncPointcutAdvisor(@Autowired LocalMessageAnnotationInterceptor localMessageAnnInterceptor){
        return new DefaultPointcutAdvisor(
                new AnnotationMatchingPointcut(null, LocalMessage.class),
                localMessageAnnInterceptor);
    }
}
